import { create } from 'domain'
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Unique,
  PrimaryColumn,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn
} from 'typeorm'

@Entity('menu')
export class Menu {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ name: 'parent_id', nullable: true })
  parentId: number

  @Column()
  zhName: string

  @Column()
  @Unique('name', ['name'])
  name: string

  @Column()
  path: string

  @Column({ nullable: true })
  icon: string

  @Column({ nullable: true })
  order: number

  @Column({ nullable: true })
  component: string

  @Column({ nullable: true })
  description: string

  @CreateDateColumn({ type: 'timestamp' })
  createTime: Date

  @UpdateDateColumn({ type: 'timestamp' })
  updateTime: Date

  constructor(menu?: Partial<Menu>) {
    Object.assign(this, menu)
  }
}

@Entity('role_menu')
@Unique('role_menu_unique', ['menuId', 'operations'])
export class RoleMenu {
  @PrimaryColumn({ name: 'role_id' })
  roleId: number

  @PrimaryColumn({ name: 'menu_id' })
  menuId: number

  @Column({ nullable: true })
  operations: string

  @Column({ nullable: true })
  permissions: string

  /**
   * 0: 菜单未分配 1: 菜单分配
   */
  @Column({ default: 0 })
  status: number

  @ManyToOne(() => Menu, (menu) => menu.id)
  @JoinColumn({ name: 'menu_id' })
  menu: Menu

  constructor(roleMenu?: Partial<RoleMenu>) {
    Object.assign(this, roleMenu)
  }
}

@Entity('menu_operation')
@Unique('menu_operation', ['menuId', 'operations'])
export class MenuOperation {
  @PrimaryColumn({ name: 'menu_id' })
  @Unique('menu_id', ['menuId'])
  menuId: number

  @Column({ nullable: true })
  operations: string

  constructor(menuToOperation?: Partial<MenuOperation>) {
    Object.assign(this, menuToOperation)
  }
}

export enum PermissionType {
  GET = 'get',
  POST = 'post',
  DELETE = 'delete',
  PATCH = 'patch'
}
@Entity('menu_permission')
@Unique('menu_permission', ['menuId', 'permission', 'type'])
export class MenuPermission {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ name: 'menu_id' })
  menuId: number

  // 接口名称
  @Column({ nullable: true })
  name: string
  // 接口标识
  @Column({ nullable: false })
  permission: string
  // 接口类型
  // 权限类型（枚举值：get、post、delete、patch）
  @Column({
    nullable: false,
    type: 'enum',
    enum: PermissionType
  })
  type: PermissionType
  // 接口描述
  @Column({ nullable: true })
  description: string

  @CreateDateColumn({ type: 'timestamp' })
  createTime: Date

  @UpdateDateColumn({ type: 'timestamp' })
  updateTime: Date
  constructor(menuPermission?: Partial<MenuPermission>) {
    Object.assign(this, menuPermission)
  }
}
